#include <pthread.h>
#include <ctime>
#include <unistd.h>
#include <cstdlib>
#include "BlockQueue.hpp"
#include "Task.hpp"

void *Consumer(void *args)
{
    BlockQueue<task_t> *bq = static_cast<BlockQueue<task_t> *>(args);
    while (1)
    {
        //sleep(2);
        // 1.获取数据
        //int date = 0;
        //Task t;
        task_t t;
        bq->Pop(&t);

        // 2.处理数据/任务
        //t.Excute();
        t();
       // std::cout << "Consumer  -> " << t.result() << std::endl;
    }
}

void *Productor(void *args)
{
    srand(time(nullptr) ^ getpid());

    BlockQueue<task_t> *bq = static_cast<BlockQueue<task_t> *>(args);
    while (1)
    {
        sleep(2);
        // 1.构建数据/任务
        //int date = rand() % 10 + 1; // [1, 10]

        // int x = rand() % 10 + 1; // [1, 10]
        // usleep(x * 1000);
        // int y = rand() % 10 + 1; // [1, 10]
        // Task t(x, y);


        // 2.生成数据
        bq->Equeue(Download);
        std::cout << "Productor -> " << "Download" << std::endl;
    }
}

int main()
{
    BlockQueue<task_t> *bq = new BlockQueue<task_t>();

    pthread_t c, p;
    pthread_create(&c, nullptr, Consumer, bq);
    pthread_create(&p, nullptr, Productor, bq);

    pthread_join(c, nullptr);
    pthread_join(p, nullptr);

    return 0;
}